package com.neusoft.controller;

import com.neusoft.entity.JsonReturnModel;
import com.neusoft.entity.ProductOfShopCart;
import com.neusoft.entity.ShopCart;
import com.neusoft.service.ShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/shopcart")
public class ShopCartController {

    @Autowired
    public ShopCartService shopCartService;

    /**
     * 把产品加入购物车
     * @param shopCartId
     * @param productId   加入购物车的商品
     * @param totalNumber 商品数量
     * @return
     */
    @RequestMapping(value="/addproductintosc",method= RequestMethod.POST)
    @ResponseBody
    public JsonReturnModel addProductIntoShopCart(
            @RequestParam(value="shopCartId",required=true) Integer shopCartId,
            @RequestParam(value="productId",required=true) Integer productId,
            @RequestParam(value="totalNumber",required=true) Integer totalNumber){
        JsonReturnModel jm = new JsonReturnModel();

        ShopCart c = shopCartService.findShopCartById(shopCartId);
        if(c==null){
            jm.setCode(201);
            jm.setMsg("该用户的购物车不存在");
            jm.setObject(null);
            jm.setSuccess(false);
        }
        //TODO 检查要加入购物车的商品是否存在，步骤同上
        Map<String,Object> map1 = new HashMap<>();
        map1.put("shopCartId",shopCartId);
        map1.put("productId",productId);
        ProductOfShopCart ps = shopCartService.findProdutOfShopCart(map1);
        if(ps!=null){
            //存在则更新购物车商品数量
            ps.setProductOfShopCart_id(shopCartId);
            ps.setProductId(productId);
            ps.setTotalNumber(ps.getTotalNumber()+totalNumber);
            Integer i = shopCartService.addProductCount(ps);
            if(i>0){
                jm.setCode(200);
                jm.setMsg("该商品已存在于购物车，已更新商品数量");
                jm.setObject(shopCartService.findShopCartById(shopCartId));
                jm.setSuccess(true);
            }else{
                jm.setCode(201);
                jm.setMsg("更新商品数量失败");
                jm.setObject(null);
                jm.setSuccess(false);
            }
        }else{
            ps.setProductOfShopCart_id(shopCartId);
            ps.setProductId(productId);
            ps.setTotalNumber(totalNumber);
            Integer i = shopCartService.addProductIntoShopCart(ps);
            if(i>0){
                jm.setCode(200);
                jm.setMsg("添加商品进购物车成功");
                jm.setObject(shopCartService.findShopCartById(shopCartId));
                jm.setSuccess(true);
            }else{
                jm.setCode(201);
                jm.setMsg("添加商品进购物车失败");
                jm.setObject(null);
                jm.setSuccess(false);
            }
        }
        return jm;
    }

    /**
     * 给购物车商品添加数量
     * @param shopCartId
     * @param productId
     * @param totalNumber
     * @return
     */
    @RequestMapping(value="/addProductCount",method= RequestMethod.POST)
    @ResponseBody
    public JsonReturnModel addProductCount(
            @RequestParam(value="shopCartId",required=true) Integer shopCartId,
            @RequestParam(value="productId",required=true) Integer productId,
            @RequestParam(value="totalNumber",required=true) Integer totalNumber){
        JsonReturnModel jm = new JsonReturnModel();

        ShopCart c = shopCartService.findShopCartById(shopCartId);
        if(c==null){
            jm.setCode(201);
            jm.setMsg("该用户的购物车不存在");
            jm.setObject(null);
            jm.setSuccess(false);
        }
        //TODO 检查要加入购物车的商品是否存在，步骤同上

        //检查该商品是否属于该购物车
        Map<String,Object> map1 = new HashMap<>();
        map1.put("shopCartId",shopCartId);
        map1.put("productId",productId);
        ProductOfShopCart ps = shopCartService.findProdutOfShopCart(map1);
        if(ps==null){
            jm.setCode(201);
            jm.setMsg("该购物车不存在该商品");
            jm.setObject(null);
            jm.setSuccess(false);
        }else{
            ps.setTotalNumber(totalNumber+ps.getTotalNumber());
            Integer i = shopCartService.addProductCount(ps);
            if(i>0){
                jm.setCode(200);
                jm.setMsg("成功更新购物车商品数量");
                jm.setObject(shopCartService.findShopCartById(shopCartId));
                jm.setSuccess(true);
            }else{
                jm.setCode(201);
                jm.setMsg("更新商品数量失败");
                jm.setObject(null);
                jm.setSuccess(false);
            }
        }
        return jm;
    }



}
